<template>
  <div class="header">
    <div
      v-for="(order, index) in orders"
      :key="index"
      :class="['order', orderIndex === index ? 'active' : '']"
      @click="switchOrder(index)"
    >
      {{order.text}}
      <i></i>
    </div>
    <div class="checkbox">
      <van-checkbox v-model="certification" @change="getData" icon-size="1.2rem">
        <i class="huobo huobo-certification"></i>
        <span class="text">海博认证</span>
      </van-checkbox>
    </div>
    <div class="checkbox">
      <van-checkbox v-model="guarantee" @change="getData" icon-size="1.2rem">
        <i class="huobo huobo-protect"></i>
        <span class="text">海博担保</span>
      </van-checkbox>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CompanyHeader',
  data() {
    return {
      orderIndex: 0,
      orders: [
        { key: 'default', text: '默认排序' },
        { key: 'newIn', text: '最新加入' },
      ],
      certification: false,
      guarantee: false
    };
  },
  methods: {
    // 切换排序
    switchOrder(index) {
      this.orderIndex = index;
      this.getData();
    },
    // 请求数据
    getData() {
      const params = {
        order: this.orders[this.orderIndex].key,
        certification: this.certification,
        guarantee: this.guarantee
      };
      console.log(params);
      this.$api.getCompanyData(params).then(data => {
        this.$emit('done', data);
      }).catch(() => {
        this.$emit('done', []);
      });
    }
  }
};
</script>

<style lang="less" scoped>
  @import '../../../less/global.less';
  .header {
    display: flex;
    margin-bottom: 10px;
    height: 4rem;
    line-height: 4rem;
    border: 1px solid #ddd;
    color: #333;
    background-color: #fff;
    .order {
      flex: 0 0 6rem;
      height: 100%;
      text-align: center;
      cursor: pointer;
      &.active {
        color: #fff;
        background-color: @mainBgColor;
      }
    }
    .checkbox {
      display: flex;
      margin-left: 1rem;
      .text {
        padding-left: 5px;
        color: #666;
      }
    }
  }
</style>
